package org.czl.leetcode4;

import java.util.LinkedList;
import java.util.Queue;

public class l1298 {
    public static void main(String[] args) {
        //status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
        System.out.println(maxCandies(new int[]{1, 0, 1, 0}, new int[]{7, 5, 4, 100}, new int[][]{{}, {}, {1}, {}},
                new int[][]{{1, 2}, {3}, {}, {}}, new int[]{0}));
    }
    public static int maxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes) {
        int n = status.length;
        Queue<Integer> queue = new LinkedList<>();
        boolean[] set = new boolean[n + 1];//检查当前盒子是否打开过
        boolean[] key = new boolean[n + 1];//表示当前有哪些钥匙
        for(int i = 0; i < initialBoxes.length; i++){
            if(status[initialBoxes[i]] == 1){
                queue.add(initialBoxes[i]);
                set[initialBoxes[i]] = true;
                key[initialBoxes[i]] = true;
            }
        }
        int ans = 0;
        while(queue.size() > 0){
            int cur = queue.poll();
            if(key[cur] || status[cur] == 1){
                ans += candies[cur];
            }else{
                continue;
            }
            for(int c : keys[cur]){
                key[c] = true;
            }

            for(int b : containedBoxes[cur]){
                if(!set[b]){
                    queue.add(b);//把当前盒子放入队列
                    set[b] = true;
                }
            }
        }
        return ans;



    }

}
